﻿using System;
using System.Xml;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class index : System.Web.UI.Page
{
    private void LoadWebSiteByXml()
    {
        DataSet dst = new DataSet();

        dst.ReadXml(MapPath(ConfigurationManager.AppSettings["XMLFILEName"]));

        if (dst != null)
        {
            this.dropWebSite.DataTextField = "Name";
            this.dropWebSite.DataValueField = "Address";
            this.dropWebSite.DataSource = dst;
            this.dropWebSite.DataBind();
        }
        
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.LoadWebSiteByXml();
        }
        else
        {
            this.lblJs.Text = "";
        }
    }

    protected void ibtnUserLogin_Click(object sender, ImageClickEventArgs e)
    {
        string strUserID = this.txtUserID.Text.Trim();

        if (strUserID != "")
        {
            string strUserPwd = this.txtUserPwd.Text.Trim();

            ClsUserLoginDAL cls_UserLogin = new ClsUserLoginDAL();

            int intCount = cls_UserLogin.SysUserIDIsExist(strUserID);

            if (intCount > 0)
            {
                if (strUserPwd != "")
                {
                    string strC_Name = this.Page.Request.ServerVariables[5];
                    string strC_Ip = "";

                    if (Page.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null)
                    {
                        strC_Ip = Page.Request.ServerVariables["Remote_Addr"];
                    }
                    else
                    {
                        strC_Ip = Page.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                    }

                    intCount = cls_UserLogin.SysUserLogin(strUserID, strUserPwd, strC_Name, strC_Ip, 0);

                    if (intCount > 0)
                    {
                        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
                        cls_Property.SetOrGetUserID = strUserID;

                        this.Page.Response.Redirect("main.aspx");
                    }
                    else if (intCount == 0)
                    {
                        this.lblJs.Text = "<script>alert('登录密码错误！')</script>";
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('登录失败，请重试！')</script>";
                    }
                }
                else
                {
                    this.lblJs.Text = "<script>alert('用户密码不能为空！')</script>";
                }
            }
            else if (intCount == 0)
            {
                this.lblJs.Text = "<script>alert('抱歉！无此帐号。')</script>";
            }
            else if (intCount == -1)
            {
                this.lblJs.Text = "<script>alert('此帐号已停用，请与管理员联系！')</script>";
            }
            else if (intCount == -2)
            {
                this.lblJs.Text = "<script>alert('此帐号已过期，请与管理员联系！')</script>";
            }
            else
            {
                this.lblJs.Text = "<script>alert('服务器繁忙，请稍后再试！')</script>";
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('用户帐号不能为空！')</script>";
        }
    }

    protected void dropWebSite_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strWebAddress = this.dropWebSite.SelectedItem.Value;

        if (strWebAddress.Trim() != "")
        {
            this.lblJs.Text =  "<script>window.open('" + strWebAddress + "')</script>";
        }
    }
}
